package com.itheima.tlias84.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.itheima.tlias84.config.AliyunOssConfig;
import com.itheima.tlias84.config.OssClient;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.annotation.Resources;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
@SuppressWarnings("all")
public class UploadController {
    /**
     * MultipartFile封装的用于接受文件的对象 是spring提供的 底层是IO流
     * 这里的file是前端的file 必须要对应
     * @param file
     * @return
     */

    @Resource
    AliyunOssConfig config;
    @Resource
    OssClient ossClient;
    @PostMapping
    public ResultUtil upload(MultipartFile file) throws IOException {
//        log.info("文件上传:{}",file);
//        log.info("文件大小:{}",file.getSize());

        //获取参数名
        log.info("文件名:{}",file.getName());
        //获取实际上传文件名
        log.info("文件名:{}",file.getOriginalFilename());// summer哥.png

        //获取实际上传的文件名
        String originalFilename = file.getOriginalFilename();
        String url=ossClient.upload(originalFilename,file.getInputStream());
        return ResultUtil.success("上传成功",url);

        /**
         * 这里是转到本地 被替换 转运到阿里云
         *   try {
         *             String path = "G:/"+uuid+suffix;
         *             //转移文件
         *             file.transferTo(new File(path));
         *             return ResultUtil.success(new StringBuffer(path));
         *         } catch (IOException e) {
         *             throw new RuntimeException(e);
         *         }
         */



    }


}
